﻿#region Copyright (C) 2008, 2009 StreamTv

/* 
 *      Copyright (C) 2008, 2009 StreamTv
 *      http://code.google.com/p/mpstreamtv/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace StreamTv2
{
    public class RequestDetails
    {
        private String moduleName;

        private String hostname;
        private int httpPort;
        private System.Collections.Specialized.NameValueCollection query;

        public String ModuleName 
        {
            get { return moduleName; }
        }

        public String HostName 
        {
            get { return hostname; }
        }

        public int ServerPort
        {
            get { return httpPort; }
        }

        public System.Collections.Specialized.NameValueCollection Query 
        {
            get { return query; }
        }

        public RequestDetails(System.Collections.Specialized.NameValueCollection requestQuery, XDocument localFile, String host, int port)
        {
            try
            {
                // Setup the inital query value.
                if (requestQuery != null)
                    query = requestQuery;
                else
                    query = new System.Collections.Specialized.NameValueCollection();

                // Get the module name.
                moduleName = localFile.Element("module").Attribute("name").Value;

                // Read all the default values out of the local file.
                var values = (from items in localFile.Descendants("option")
                              select new
                              {
                                  Name = items.Attribute("name").Value,
                                  Value = items.Value,
                                  Force = Boolean.Parse(items.Attribute("changeable").Value)
                              }).ToList();

                foreach (var item in values)
                {
                    if (item.Force)
                    {
                        if (query.AllKeys.Contains(item.Name))
                            query.Set(item.Name, item.Value);
                        else
                            query.Add(item.Name, item.Value);
                    }
                    else
                    {
                        if (!query.AllKeys.Contains(item.Name))
                            query.Add(item.Name, item.Value);
                    }
                }
            }
            catch (Exception)
            {
                query = null;
                moduleName = "";
            }

            hostname = host;
            httpPort = port;
        }
    }
}
